package com.xr.pfm.template;

import java.util.HashMap;
import java.util.List;

import com.xr.common.CommUtil;
import com.xr.common.Tools;
import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.utils.variant.VariantSet;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * batchSetEmpPfmViewModel
 */
public class batchSetEmpPfmViewModel extends DefaultViewModel {
	protected void doUpdateData(ParameterSet parameters,
			ParameterSet outParameters) throws Exception {
		Dataset dsEmp = getDataset("datasetEmp");
		Dataset dsTp = getDataset("datasetTp");
		String templateId = dsTp.getString("PFM_TEMPLATE_ID");
		DBStatement dbs = new DBStatement();
		HashMap<String, String> hm = new HashMap();
		String sql = "";
		String pfmResultId = "";
		dsEmp.moveFirst();
		while (!dsEmp.isLast()) {
			pfmResultId = CommUtil.getNewId("general");
			CommUtil.executeUpdate(
							dbs,
							"insert into pfm_result(pfm_result_id,ONOFF_STATION_ID,examine_emp_id, assessor,create_id, if_can_check,CHECK_FROM_DATE,CHECK_TO_DATE)"
									+ "values("
									+ pfmResultId
									+ ","
									+ dsEmp.getString("ONOFF_STATION_ID")
									+ ","
									+ dsEmp.getString("EMPLOYEE_ID")
									+ ","
									+ dsTp.getString("ASSESSOR")
									+ ",1,"
									+ dsTp.getString("if_can_check")+",to_date('"+Tools.format(dsTp.getDate("CHECK_FROM_DATE"),"yyyy-MM-dd")+"','yyyy-MM-dd'),to_date('"+Tools.format(dsTp.getDate("CHECK_TO_DATE"),"yyyy-MM-dd")+"','yyyy-MM-dd')" + ")");
			dbs.setSql("select * from PFM_TEMPLATE_ITEM_DEMO where PFM_TEMPLATE_ID="
							+ templateId);
			List list = dbs.queryForList();
			for (int i = 0; i < list.size(); i++) {
				VariantSet vs = (VariantSet) list.get(i);
				hm.put(vs.getString("PFM_TEMPLATE_ITEM_ID"), CommUtil
						.getNewId("general"));

				if (vs.getString("SUPER_TEMPLATE_ITEM_ID") != null
						&& hm.get(vs.getString("SUPER_TEMPLATE_ITEM_ID")) != null) {
					hm.put(vs.getString("SUPER_TEMPLATE_ITEM_ID"), hm.get(vs
							.getString("SUPER_TEMPLATE_ITEM_ID")));
				}

				sql = "insert into pfm_checkitem_demo"
						+ "(checkitem_id, pfm_result_id, super_checkitem_id, check_target, check_detail, score, weight, formula_for_score, formula_for_score_show, ASSESSOR,START_KPI,PLAN_KPI,CHALLENGE_KPI)"
						+ "select "
						+ hm.get(vs.getString("PFM_TEMPLATE_ITEM_ID"))
						+ ","
						+ pfmResultId
						+ ","
						+ hm.get(vs.getString("SUPER_TEMPLATE_ITEM_ID"))
						+ ",pid.check_target,pid.check_detail,pid.score,pid.weight,pid.formula_for_score,pid.formula_for_score_show,"
						+ dsTp.getString("ASSESSOR")
						+ ",START_KPI,PLAN_KPI,CHALLENGE_KPI from PFM_TEMPLATE_ITEM_DEMO pid where pid.pfm_template_item_id="
						+ vs.getString("pfm_template_item_id")+" order by PFM_TEMPLATE_ITEM_ID";
				CommUtil.executeUpdate(dbs, sql);

			}
			
			//pfmResultId
			if(dsTp.getString("IF_CAN_CHECK").equals("1")){
				CommUtil.executeUpdate(dbs, "begin pck_pfm.startPfm("+CommUtil.getEmployeeId()+","+pfmResultId+"); end;");
			}
			
			dsEmp.moveNext();
		}
		dbs.close();
		// super.doUpdateData(parameters, outParameters);
	}

}
